package com.fs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.pojo.Inventory;
import com.fs.pojo.WhDetailed;
import com.fs.pojo.WhOutbound;
import com.fs.pojo.WhWarehousing;
import com.fs.service.WhDetailedService;
import com.fs.service.WhOutboundService;
import com.fs.util.ResponseDataUtil;
import com.fs.vo.Condition;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reserve")
public class WhoutBoundController {

    @Autowired
    private WhOutboundService whOutboundService;

    @Autowired
    private WhDetailedService whDetailedService;

    /**
     * 根据条件查询出库单列表（分页）。
     *
     * @param condition 查询条件
     * @return 包含出库单分页数据的响应
     */
    @PostMapping("/outbound/list")
    public PageInfo<WhOutbound> selectPage(@RequestBody Condition<WhOutbound> condition) {
        System.out.println(condition);
        return whOutboundService.selectPage(condition);
    }



    /**
     * 根据出库单编号获取出库单。
     *
     * @param outboundEntryId 出库单编号
     * @return 包含出库单数据的响应
     */
    @DeleteMapping("/outbound/{outboundEntryId}")
    public ResponseData<String> deleteOutbound(@PathVariable String outboundEntryId) {
        try {
            if (outboundEntryId.contains(",")) {
                // 批量删除
                String[] ids = outboundEntryId.split(",");
                for (String id : ids) {
                    whOutboundService.deleteWhOutbound(Integer.parseInt(id));
                }
                return new ResponseData<>(ResultEnums.OK, "批量删除成功");
            } else {
                // 单个删除
                whOutboundService.deleteWhOutbound(Integer.parseInt(outboundEntryId));
                return new ResponseData<>(ResultEnums.OK, "删除成功");
            }
        } catch (Exception e) {
            return new ResponseData<>(ResultEnums.DELETED, null);
        }
    }


    /**
     * 根据出库单编号获取出库单。
     *
     * @param whOutbound 出库单对象
     * @return 包含出库单数据的响应
     */
    @PostMapping("/outbound/add")
    public ResponseData addOutbound(@RequestBody WhOutbound whOutbound){
        Integer result = whOutboundService.addWhOutbound(whOutbound);
        return result !=0 ? ResponseDataUtil.buildOk("200","新增成功"):ResponseDataUtil.buildFail(null);
    }

    /**
     * 根据出库单编号修改出库单。
     *
     * @param whOutbound 出库单对象
     * @return 包含出库单数据的响应
     */
    @PutMapping("/outbound/update")
    public ResponseData updateOutbound(@RequestBody WhOutbound whOutbound){
        Integer result = whOutboundService.updateWhOutbound(whOutbound);
        return result!=0? ResponseDataUtil.buildOk("200","修改成功"):ResponseDataUtil.buildFail(null);
    }



    /**
     * 根据条件查询出库明细单列表（分页）。
     * @param condition 查询条件
     * @return 包含出库单分页数据的响应
     */
    @PostMapping("/outbound/wdlist")
    public ResponseData<PageInfo> selectPageWD(@RequestBody Condition<WhDetailed> condition){
        return new ResponseData<PageInfo>(ResultEnums.OK,whDetailedService.selectWD(condition));
    }

}